package com.example.a20112.ui_demo.model;


import com.example.a20112.ui_demo.beans.LoginBean;
import com.example.a20112.ui_demo.beans.RegisterBean;
import com.example.a20112.ui_demo.listener.RetrofitListener;
import com.example.a20112.ui_demo.retrofitservice.UserService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by asus on 2018/9/13.
 */

public class UserModel extends RetrofitBaseModel{
    private Retrofit retrofit;
    private static final String BASE_URL="http://172.24.10.175:8080/foodService/";
    private UserService userService;
    public UserModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        this.userService=retrofit.create(UserService.class);
    }
    public void userLogin(String usernameview,String passwordview,final RetrofitListener listener){
        Call call=userService.login(usernameview,passwordview);
        Callback<LoginBean>callback =new Callback<LoginBean>(){

            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                listener.onSuccess(response.body(),1);
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
listener.onFail();
            }
        };
        bindCallback(call,listener,Constants.USERLOGIN);
    }
    public void register(String username,String password1,String address,String phonenumber,String comment,final RetrofitListener listener){
        Call call=userService.register(username,password1,address,phonenumber,comment);
        Callback<RegisterBean>callback =new Callback<RegisterBean>(){

            @Override
            public void onResponse(Call<RegisterBean> call, Response<RegisterBean> response) {
                listener.onSuccess(response.body(),1);
            }

            @Override
            public void onFailure(Call<RegisterBean> call, Throwable t) {
                listener.onFail();
            }
        };
        bindCallback(call,listener,Constants.USERREGISTER);
    }
/*店铺列表*/
    public void getAllShops(RetrofitListener listener) {
        Call call = userService.getAllShops();
        bindCallback(call,listener, Constants.GETALLSHOPS);

    }
    /*菜品列表*/
    public void getFoodByShop(int shop_id, RetrofitListener listener){
        Call call = userService.getFoodByShop(shop_id);
        bindCallback(call,listener, Constants.GETFOODBYSHOP);
    }

    /*收藏*/
    public void isCollected(int user_id,int shop_food_id,int flag,RetrofitListener listener){
        Call call=userService.isCollected(user_id,shop_food_id,flag);
        bindCallback(call,listener,Constants.ISCOLLECTED);
    }
}
